<div class="layui-fluid">
    <div class="layui-card">
<!--        <div class="layui-form layui-card-header layuiadmin-card-header-auto">-->
<!--            <div class="layui-form-item">-->
<!--                <div class="layui-inline">-->
<!--                    <label class="layui-form-label">登录名</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-inline">-->
<!--                    <label class="layui-form-label">手机</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" name="mobile" placeholder="请输入" autocomplete="off" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-inline">-->
<!--                    <label class="layui-form-label">邮箱</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" name="email" placeholder="请输入" autocomplete="off" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-inline">-->
<!--                    <label class="layui-form-label">角色</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select name="role">-->
<!--                            <option value="0">管理员</option>-->
<!--                            <option value="1">超级管理员</option>-->
<!--                            <option value="2">纠错员</option>-->
<!--                            <option value="3">采购员</option>-->
<!--                            <option value="4">推销员</option>-->
<!--                            <option value="5">运营人员</option>-->
<!--                            <option value="6">编辑</option>-->
<!--                        </select>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-inline">-->
<!--                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">-->
<!--                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>-->
<!--                    </button>-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="刷新" data-type="refresh"><i
                        class="layui-icon layui-icon-refresh-3"></i>
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('administration/download')?$auth->check('administration/download'):'hide'}" data-type="export" title="导出"><i
                        class="iconfont icon-daochu"></i>导出
                </button>
            </div>
            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
        </div>
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    var listUrl = "{:url('administration/ChangeTable')}",downloadExportUrl = "{:url('administration/download')}";
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'soulTable'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table, soulTable = layui.soulTable;
        table.render({
            elem: "#LAY-user-back-manage", url: listUrl
            , cellMinWidth: 100
            , overflow: {
                type: 'tips'
                // ,hoverTime: 500 // 悬停时间，单位ms, 悬停 hoverTime 后才会显示，默认为 0
                , color: 'white' // 字体颜色
                , bgColor: '#009688' // 背景色
            }
            , cols: [
                [
                    {type: "checkbox", fixed: "left"},
                    {field: "id", align: "center", title: "id"},
                    {field: "username", align: "center", title: "用户名"},
                    {field: "nickname", align: "center", title: "昵称"},
                    {field: "rules", align: "center", title: "所属组别"},
                    {field: "mobile", align: "center", title: "手机号"},
                    {field: "email", align: "center", title: "邮箱"},
                    {field: "birthday_num", align: "center", title: "生日"},
                ]],
            done: function () {
                soulTable.render(this)
            },
            response: {
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            },
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code,
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            text: {
                'none': "呀！一条数据都没有？"
            },
            page: true,
            limit: 15,
        });
        //按钮触发
        table.on("tool(LAY-user-back-manage)", function (e) {
            var param = $.param({'id': e.data.id});
            if ("edit" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '编辑'
                    , content: edit + '?' + param
                    , area: ['50%', '50%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: edit,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            } else if ("del" === e.event) {
                var params = $.param({'ids': e.data.id});
                layer.confirm("确定删除此用户吗？", function (t) {
                    $.ajax({
                        url: deleteUrl + '?' + params, success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-user-back-manage');
                                layer.close(t); //关闭弹层
                            } else {
                                console.log(res);
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                })
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-user-back-manage', {
                where: field
            });
        });

        //事件
        var active = {
            export: function () {
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    , checkData = checkStatus.data; //得到选中的数据
                var id = ''; //task_id
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                }
                id = id.substring(0, id.length - 1);
                layer.confirm('确定导出数据吗？', function (index) {
                    //执行 Ajax 后重载
                    $.ajax({
                        url: downloadExportUrl,
                        type: "post",
                        data: {
                            ids: id
                        },
                        success: function (res) {
                            if (res.code === 400) {
                                layer.msg(res.message, {icon: 5}, function () {
                                    layer.closeAll();
                                });
                                return false;
                            } else {
                                var dataObj = eval("(" + res + ")");
                                if (dataObj.code == 200) {
                                    layer.closeAll();
                                    window.location.href = dataObj.url;
                                } else {
                                    layer.msg("暂无查询到有效数据", {icon: 5});
                                    return false;
                                }
                            }
                        }
                    });
                    // table.reload('LAY-user-back-manage');
                    // layer.msg('已删除');
                });
            }
        };
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>